// 统一管理整个项目所有的验证规则
export const loginRules = {
  // required：必填 mes提示信息 trigger什么时候触发 blur 失去焦点触发
  mobile: [{
    required: true,
    pattern: /^1[3|4|5|6|7|8|9]\d{9}$/,
    message: '输入正确的手机号',
    trigger: 'blur'
  }],
  code: [{ required: true, min: 6, max: 6, message: '验证码错误', trigger: 'change' }],
  check: [{
    trigger: 'change',
    // 正则只能判断字符串 无法判断布尔值 所以自定义规则
    validator: (rule, value, callback) => {
      if (!value) {
        callback(new Error('请勾选协议'))
      } else {
        callback()
      }
    }
  }]
}

// 发布文章内的
export const articleAddRules = {
  title: [ // 规则名(必须和v-model的变量名一致)
    {
      min: 5,
      max: 30,
      required: true,
      message: '标题5到30字符',
      trigger: 'blur' // 在失去焦点触发验证规则
    }
  ],
  content: [
    { required: true, message: '内容必须填写', trigger: 'blur' }
  ],
  channel_id: [
    {
      required: true,
      trigger: 'blur',
      message: '频道必须选择'
    }
  ],
  cover: [{
    validator: (rule, value, callback) => {
      if (value.images.indexOf(undefined) > -1) callback(new Error('图片数量有误'))
      else if (value.type === value.images.length || value.type === -1) callback()
      else callback(new Error('图片数量有误'))
    }

  }]
}

// 更新用户信息
export const updateUserRules = {
  name: [
    {
      min: 1,
      max: 7,
      required: true,
      message: '昵称1-7个字符',
      trigger: 'blur'
    }
  ],
  email: [{
    type: 'email',
    required: true,
    message: '输入合法的邮箱',
    trigger: 'blur'
  }
  ]
}
